package com.ymatou.iapi.optrade.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.DomesticDeliveryReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.SyncDomesticDeliveryCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 同步同步国内发货信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SyncDomesticDelivered {

	private static DomesticDeliveryReq domesticDeliveryReq;
	private static SyncDomesticDeliveryCall domesticDeliveryCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("同步国内发货信息接口");
	}

	@Before
	public void caseUp() {
		domesticDeliveryReq = new DomesticDeliveryReq();
		domesticDeliveryCall = new SyncDomesticDeliveryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(domesticDeliveryCall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 校验自动确认收货时间操作表（auto_receive_opt） 
	 */
	private void verifyAutoReceive() {

		long orderId = domesticDeliveryReq.getOrderId();

		// 订单表(order)
		OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(3, ymtOrders.getOrderStatus(), "验证orderStatus");
		Logger.verifyEquals(false, ymtOrders.getXloboDelivery(),
				"验证xloboDelivery");

		Logger.verifyEquals(true, ymtOrders.getDomesticDelivery(),
				"验证domesticDelivery");

		// 订单表(order)
		AutoReceiveOptPo autoReceiveOptPo = orderShardingWapper
				.selectAutoReceiveOptByOrderId(orderId);

		Logger.comment(String.format(
				"验证自动确认收货时间操作表（auto_receive_opt）  orderId: %s", orderId));

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, autoReceiveOptPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(false, autoReceiveOptPo.getFreeze(), "验证数据库Freeze");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getFreezeTime()) < 2,
				"验证数据库FreezeTime跟系统当前时间差为2分钟内");

		/*		Logger.verifyEquals(null, autoReceiveOptPo.getExpectReceiveTime(),
						"验证数据库ExpectReceiveTime");*/

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDelivered_001() {
		Logger.start(true, "同步国内发货信息成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();

			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId, LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);

			Logger.comment("前置数据准备结束");

			domesticDeliveryReq.setLogisticsCompany("码头自动化测试物流");
			domesticDeliveryReq.setOrderId(orderId);
			domesticDeliveryReq.setLogisticsNo("autotest_"
					+ TestDataManager.getRandomNum(5));
			domesticDeliveryCall.setData(domesticDeliveryReq);
			domesticDeliveryCall.callService();

			Logger.verifyEquals("ok", domesticDeliveryCall.getReturnData(),
					"验证接口返回");

			verifyAutoReceive();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDelivered_002() {
		Logger.start(true, "订单号不存在");
		try {
			domesticDeliveryReq.setLogisticsCompany("码头自动化测试物流");
			domesticDeliveryReq.setOrderId(1111);
			domesticDeliveryReq.setLogisticsNo("22222222222");

			domesticDeliveryCall.setData(domesticDeliveryReq);
			domesticDeliveryCall.callService();
			Logger.verifyEquals("ok", domesticDeliveryCall.getReturnData(),
					"验证接口返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDelivered_003() {
		Logger.start(true, "订单号为空");
		try {
			domesticDeliveryReq.setLogisticsCompany("码头自动化测试物流");
			// domesticDeliveryReq.setOrderId(1111);
			domesticDeliveryReq.setLogisticsNo("22222222222");

			domesticDeliveryCall.setData(domesticDeliveryReq);
			domesticDeliveryCall.callService();

			Logger.verifyEquals("fail", domesticDeliveryCall.getReturnData(),
					"验证接口返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDelivered_004() {
		Logger.start(true, "多物流单号,同步国内发货信息成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId, LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

			Logger.comment("前置数据准备结束");

			domesticDeliveryReq.setLogisticsCompany("码头自动化测试物流");
			domesticDeliveryReq.setOrderId(orderId);
			domesticDeliveryReq.setLogisticsNo("autotest_"
					+ TestDataManager.getRandomNum(5)
					+ ",sdfsdf,qwerwe,autotestno");

			domesticDeliveryCall.setData(domesticDeliveryReq);
			domesticDeliveryCall.callService();

			Logger.comment("同步国内发货信息前");

			// 验证订单表
			Logger.verifyEquals("ok", domesticDeliveryCall.getReturnData(),
					"验证接口返回");

			verifyAutoReceive();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDelivered_005() {
		Logger.start(true, "重复同步国内发货信息成功");
		try {

			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId, false);

			Logger.comment("前置数据准备结束");

			domesticDeliveryReq.setLogisticsCompany("码头自动化测试物流");
			domesticDeliveryReq.setOrderId(orderId);
			domesticDeliveryReq.setLogisticsNo("autotest_"
					+ TestDataManager.getRandomNum(5));
			domesticDeliveryCall.setData(domesticDeliveryReq);
			domesticDeliveryCall.callService();
			Logger.comment("重复同步国内发货");
			domesticDeliveryCall.callService();

			Logger.verifyEquals("ok", domesticDeliveryCall.getReturnData(),
					"验证接口返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDelivered_006() {
		Logger.start(true, "订单还未国际发货");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLuxury(false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			// 拼邮国际段发货
			//TradingCallService.delivery(sellerId, orderId, false);

			Logger.comment("前置数据准备结束");

			domesticDeliveryReq.setLogisticsCompany("码头自动化测试物流");
			domesticDeliveryReq.setOrderId(orderId);
			domesticDeliveryReq.setLogisticsNo("autotest_"
					+ TestDataManager.getRandomNum(5));
			domesticDeliveryCall.setData(domesticDeliveryReq);
			domesticDeliveryCall.callService();

			Logger.verifyEquals("ok", domesticDeliveryCall.getReturnData(),
					"验证接口返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
